package com.company.project.utils;

import com.company.project.constants.Constants;
import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class StringUtil {

	public static final String EMPTY = "";

	/**
	 * 转义
	 * @param src 源字符串
	 * @return
     */
	public static String escape(String src) {
		if(src==null){
			return "";
		}
		int i;
		char j;
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length() * 6);
		for (i = 0; i < src.length(); i++) {
			j = src.charAt(i);
			if (Character.isDigit(j) || Character.isLowerCase(j)
					|| Character.isUpperCase(j)){
				tmp.append(j);
			}else if (j < 256) {
				tmp.append("%");
				if (j < 16){
					tmp.append("0");
				}
				tmp.append(Integer.toString(j, 16));
			} else {
				tmp.append("%u");
				tmp.append(Integer.toString(j, 16));
			}
		}
		return tmp.toString();
	}

	/**
	 * 还原转义字符串
	 * @param src
	 * @return
     */
	public static String unescape(String src) {
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < src.length()) {
			pos = src.indexOf("%", lastPos);
			if (pos == lastPos) {
				if (src.charAt(pos + 1) == 'u') {
					ch = (char) Integer.parseInt(src
							.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				} else {
					ch = (char) Integer.parseInt(src
							.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			} else {
				if (pos == -1) {
					tmp.append(src.substring(lastPos));
					lastPos = src.length();
				} else {
					tmp.append(src.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		return tmp.toString();
	}

	/**
	 * 随机字符串
	 * @param prefix 前缀
	 * @param len 长度
     * @return 随机字符串
     */
	public static String randomCode(String prefix, int len){
		return prefix + randomCode(len);
	}

	/**
	 * 随机字符串
	 * @param len 长度
	 * @return 随机字符串
     */
	public static String randomCode(int len){
		final int maxNum = 10;   
        int i; // 生成的随机数   
        int count = 0; // 生成的密码的长度   
        char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',  
                'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',  
                'x', 'y', 'z','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',  
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',  
                'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
           
       /* char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; */  
  
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while (count < len) {   
            // 生成随机数，取绝对值，防止生成负数
  
            i = Math.abs(r.nextInt(maxNum)); // 生成的数最大为36-1
  
            if (i >= 0 && i < str.length) {   
                pwd.append(str[i]);   
                count++;   
            }   
        }   
  
        return pwd.toString();  
	}
	/**
	 * 截断字符串
	 * @param str 源字符串
	 * @param bytelength 字节长度
	 * @param sufix 后缀
	 * @return
	 */
	public static String truncate(String str, int bytelength, String sufix) {
		if (str == null) {
			return null;
		}
		if (bytelength <= 0) {
			return "";
		}
		try {
			if (str.getBytes("GBK").length <= bytelength) {
				return str;
			}
		} catch (Exception ex) {
		}
		StringBuffer buff = new StringBuffer();

		int index = 0;
		char c;
		while (bytelength > 0) {
			c = str.charAt(index);
			if (c < 128) {
				bytelength--;
			} else {// GBK编码汉字占2字节，其他编码的不一样，要修改下面几句
				bytelength--;
				if (bytelength < 1)// 被截断了
					break;
				bytelength--;
			}
			buff.append(c);
			index++;
		}
		buff.append(sufix);
		return buff.toString();
	}

	/**
	 * 字符串转布尔值
	 * @param boolStr
	 * @return
     */
	public static boolean parseBoolean(String boolStr) {
		return "true".equals(boolStr) || "1".equals(boolStr) || "yes".equalsIgnoreCase(boolStr) || "y".equalsIgnoreCase(boolStr) || "enable".equalsIgnoreCase(boolStr);
	}

	/**
	 * 转换字符串编码 iso到utf8
	 * @param isostr
	 * @return
     */
	public static String iso2uft8(String isostr) {
		try {
			byte[] byteStr = isostr.getBytes("ISO-8859-1");
			String gbkStr = new String(byteStr, "UTF-8");
			return gbkStr;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 转换字符串编码 gbk到iso
	 * @param gbkstr
	 * @return
     */
	public static String gbk2iso(String gbkstr) {
		try {
			byte[] byteStr = gbkstr.getBytes("GB2312");
			String isoStr = new String(byteStr, "ISO-8859-1");
			return isoStr;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 拼接字符串
	 * @param list
	 * @param separator
	 * @return
     */
	public static String join(List<?> list, String separator) {
		return org.apache.commons.lang3.StringUtils.join(list, separator);
	}

	/**
	 * 拼接字符串
	 * @param list
	 * @param separator
	 * @param warpper
     * @return
     */
	public static String join(List<?> list, String separator, String warpper) {
		return join(list.toArray(),separator,warpper);
	}

	/**
	 * 拼接字符串
	 * @param list
	 * @param separator 连接符
     * @return
     */
	public static String join(Object[] list, String separator) {
		return org.apache.commons.lang3.StringUtils.join(list, separator);
	}

	/**
	 * 拼接字符串
	 * @param list
	 * @param separator 分割符
	 * @param warpper 包裹符
     * @return
     */
	public static String join(Object[] list, String separator, String warpper) {
		List<String> warpperList = new ArrayList<String>();
		for(Object o : list){
			warpperList.add(warpper+o+warpper);
		}
		return org.apache.commons.lang3.StringUtils.join(warpperList, separator);
	}

	public static boolean isNotEmpty(String str){
		return org.apache.commons.lang3.StringUtils.isNotEmpty(str);
	}

	public static boolean isEmpty(String str){
		return org.apache.commons.lang3.StringUtils.isEmpty(str);
	}
	public static boolean isBlank(String str){
		return org.apache.commons.lang3.StringUtils.isBlank(str);
	}
	public static boolean isNotBlank(String str){
		return org.apache.commons.lang3.StringUtils.isNotBlank(str);
	}

	public static String warpper(String str, String warpper){
		String result = "";
		if(isNotEmpty(str) && isNotEmpty(warpper)){
			result = warpper+str+warpper;
		}
		return result;
	}

	/**
	 * 分割字符串
	 * @param src
	 * @param separator 分割符
     * @return
     */
	public static String[] split(String src, String separator) {
		String str[] = org.apache.commons.lang3.StringUtils.split(src,separator);
		if(str == null){
			str = new String[]{};
		}
		return str;
	}

	/**
	 * 分割后再拼接字符串
	 * @param src
	 * @param srcSeparator 分割符
	 * @param separator 拼接符
     * @return
     */
	public static String split4join(String src, String srcSeparator, String separator) {
		return join(split(src,srcSeparator),separator);
		
	}

	/**
	 * 判断字符串中是否含有中文
	 * @param str
	 * @return
	 */
	public Boolean isIncludeChinese(String str){
		if(str.getBytes().length == str.length()){
			return Boolean.FALSE;
		}else{
			return Boolean.TRUE;
		}
	}

	public static String replaceIllegalChar(String s){
		s = s == null ? "" : s;
		s = s.replaceAll("\'", "\'\'");
		s = s.replaceAll("_", "\\_");
		s = s.replaceAll("%", "\\%");
		return s;
	}
	
	public static String uuid(){
		UUID uuid = UUID.randomUUID();
        return uuid.toString();
	}

	/**
	 *
	 * @param d
	 * @return
	 */
	public static String objectToString(Object d){
		//如果是double类型，去除当整数时会带.0的问题
		if(d instanceof Double){
			DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
			return decimalFormat.format(d);
		}else if(d instanceof String ){
			return (String)d;
		}
		return null;
	}

	public static boolean validValue(String value){
		if(value ==null|| "".equalsIgnoreCase(value) || Constants.EXCEL_ERROR_MESSAGE.equalsIgnoreCase(value)){
			return true;
		}
		return false;
	}

	/**
	 * 数组转String
	 * @param str 数组
	 * @return String
	 */
	public static String join(String[] str){
		if(str == null || str.length == 0){
			return "";
		}
		return StringUtils.join(str,",");
	}

    /**
     * 数组转String
     * @param str 数组
     * @return String
     */
    public static String[] split(String str){
        return split(str,",");
    }

    /**
     * String转数组
     * @param str String
     * @return String数组
     */
    public static String join(String[] str,String separator){
        if(str == null || str.length == 0){
            return "";
        }
        return StringUtils.join(str,separator);
    }
}
